Branchwell

Lumen & Gesso / website · lumengesso.com / app / api / reviews / [id] / photo / route.ts

1import { commerceDb } from '@/app/lib/commerce-db';
2import { getCustomerUser } from '@/app/lib/lumen-auth';
3import { isAdmin } from '@/app/lib/admin';
4import { reviewPhotoBucket } from '@/app/lib/review-photo-storage';
5import { isReviewId } from '@/app/lib/reviews';
6
7export const dynamic = 'force-dynamic';
8
9export async function GET(_req: Request, ctx: { params: Promise<{ id: string }> }) {
10 const { id } = await ctx.params;
11 if (!isReviewId(id)) return new Response(null, { status: 404 });
12 const admin = commerceDb();
13 const { data: review } = await admin.from('order_reviews')
14 .select('photo_path,status').eq('id', id).maybeSingle();
15 if (!review?.photo_path) return new Response(null, { status: 404 });
16
17 if (review.status !== 'approved') {
18 const user = await getCustomerUser();
19 if (!isAdmin(user?.email)) return new Response(null, { status: 404 });
20 }
21 const photo = await reviewPhotoBucket().get(review.photo_path);
22 if (!photo) return new Response(null, { status: 404 });
23 return new Response(photo.body, {
24 headers: { 'Content-Type': 'image/jpeg', 'Cache-Control': 'private, no-store', 'X-Content-Type-Options': 'nosniff' },
25 });
26}